package com.huayi.website.controller.manage;

import com.huayi.website.entity.NewsCategoryEntity;
import com.huayi.website.entity.PagesEntity;
import com.huayi.website.request.GoodsDeleteReq;
import com.huayi.website.service.NewsService;
import com.huayi.website.service.PagesService;
import com.huayi.website.utils.EnumResultVo;
import com.huayi.website.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/manage")
@Controller
public class MPageController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private PagesService pagesService;
    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/page_list", method = RequestMethod.GET)
    public String getList(HttpServletRequest request, Model model) {

        var result = pagesService.getAllPage();
        model.addAttribute("list", result);
        model.addAttribute("totalSize", result.size());

        model.addAttribute("menu","page_list");
        return "manage/page_list";
    }

    /**
     * 添加分类
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/page_add", method = RequestMethod.GET)
    public String add(HttpServletRequest request, Model model) {
        model.addAttribute("page",new PagesEntity());

        model.addAttribute("menu","news_category_list");

        return "manage/page_add";
    }

    @RequestMapping(value = "/page_edit", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, @RequestParam Integer id, Model model) {
        var page = pagesService.getById(id);
        model.addAttribute("page",page);

        model.addAttribute("menu","news_category_list");
        return "manage/page_add";
    }

    @ResponseBody
    @RequestMapping(value = "/page_add_submit", method = RequestMethod.POST)
    public ResultVo<String> publishPost(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userName = session.getAttribute("userName").toString();
        if (StringUtils.isEmpty(userName)) return new ResultVo<>(500, "请登录");


        String name = request.getParameter("name");
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)) id="0";
        String titleCn = request.getParameter("titleCn");
        String titleEn = request.getParameter("titleEn");
//        String desc = request.getParameter("desc");


        Integer pageId = pagesService.editPage(Integer.parseInt(id),name,titleCn,titleEn);

        return new ResultVo<>(0, "SUCCESS");
    }



}
